package com.example.messagemodule.listener;

import android.text.TextUtils;

import com.example.messagemodule.entity.HbasePatientImManagerEntity;
import com.example.messagemodule.manager.MessageType;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMCustomMessageBody;
import com.hyphenate.chat.EMMessage;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class IMMessageListener implements EMMessageListener {

    @Override
    public void onMessageReceived(List<EMMessage> messages) {
        receivedListener(messages, MessageListenerType.MessageReceived);
    }

    @Override
    public void onCmdMessageReceived(List<EMMessage> messages) {
        receivedListener(messages, MessageListenerType.MessageCmdReceived);
    }

    @Override
    public void onMessageRead(List<EMMessage> messages) {
        receivedListener(messages, MessageListenerType.MessageRead);
    }

    @Override
    public void onMessageDelivered(List<EMMessage> messages) {
        receivedListener(messages, MessageListenerType.MessageDelivered);

    }

    @Override
    public void onMessageRecalled(List<EMMessage> messages) {
        receivedListener(messages, MessageListenerType.MessageRecalled);

    }

    @Override
    public void onMessageChanged(EMMessage message, Object change) {

    }


    void receivedListener(List<EMMessage> messages, MessageListenerType type) {
        for (int i = 0; i < messages.size(); i++) {
            EMMessage emMessage = messages.get(i);
            switch (type) {
                case MessageReceived:
                    //收到普通消息处理
//                    判断这条消息是否是自定义消息
                    if (emMessage.getType() == EMMessage.Type.CUSTOM){
                        //获取自定义消息扩展字段 event 自定义消息类型
                        EMCustomMessageBody body = (EMCustomMessageBody) messages.get(i).getBody();
                        String event = body.event();
                        if (TextUtils.equals(event, MessageType.MessageEvent_DOCTOR_EVALUATE_THANKS)) {
                            continue;
                        }
                        if (
                                TextUtils.equals(event,MessageType.MessageEvent_RECEIVED) ||
                                TextUtils.equals(event,MessageType.MessageEvent_FINISH) ||
                                TextUtils.equals(event,MessageType.MessageEvent_REFUND) ||
                                TextUtils.equals(event,MessageType.MessageEvent_SF_FINISH) ||
                                TextUtils.equals(event,MessageType.MessageEvent_SF_SUBMIT) ||
                                TextUtils.equals(event,MessageType.MessageEvent_DELAY_CHARGE) ||
                                TextUtils.equals(event,MessageType.MessageEvent_CANCEL_CHARGES)
                        ) {
                            if (emMessage.getChatType() == EMMessage.ChatType.Chat) {
                                //发送广播刷新页面数据
                                EventBus.getDefault().post(new EventBusMessageWrap(emMessage,MessageListenerType.RefreshChat));
//                                entryIm(false);
                            } else if (emMessage.getChatType() == EMMessage.ChatType.GroupChat) {
                                EventBus.getDefault().post(new EventBusMessageWrap(emMessage,MessageListenerType.RefreshChatGroup));
//                                entryGroup(externalOrderNo);
                            }
                            if (TextUtils.equals(event,MessageType.MessageEvent_SF_SUBMIT)) {
                                EventBus.getDefault().post(new EventBusMessageWrap(emMessage,MessageListenerType.ClearMessageData));
                            }
                        }
                        if (TextUtils.equals(event,MessageType.MessageEvent_PATIENT_EVALUATE_FINISH)) {
                            EventBus.getDefault().post(new EventBusMessageWrap(emMessage,MessageListenerType.NotifyData));
                        }
                        if (TextUtils.equals(event,MessageType.MessageEvent_PATIENT_END_CHARGES)) {
                            EventBus.getDefault().post(new EventBusMessageWrap(emMessage,MessageListenerType.RefreshChatGroup));
                        }

                    }

                    break;
                case MessageCmdReceived:{

                }
                break;
            }
            EventBus.getDefault().post(new EventBusMessageWrap(emMessage,type));

        }
    }

}
